package com.kqb.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kqb.common.constant.TypeCode;
import com.kqb.common.curd.impl.BaseServiceImpl;
import com.kqb.common.domain.AjaxResult;
import com.kqb.common.domain.page.PageResult;
import com.kqb.mapper.CustomerMapper;
import com.kqb.mapper.CustomerUserMapper;
import com.kqb.pojo.Customer;
import com.kqb.pojo.User;
import com.kqb.pojo.dto.CustomerDTO;
import com.kqb.service.CustomerService;
import com.kqb.utils.ThreadLocalUtils;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerServiceImpl extends BaseServiceImpl<Customer, CustomerMapper> implements CustomerService {

    @Autowired
    CustomerMapper customerMapper;
    @Autowired
    CustomerUserMapper customerUserMapper;

    /**
     * 添加客户
     * @param customerDTO
     * @return
     */
    @Transactional
    @Override
    @ApiOperation("添加客户") // OK 成功
    public Map<String, Object> save(CustomerDTO customerDTO) {
        //code 1 表示成功 0 表示失败
        //返回的对象
        HashMap<String, Object> map = new HashMap<>();
        //1.先创建要添加的user对象
        User user = new User(customerDTO.getAccount(), customerDTO.getPassword(), TypeCode.CUSTOMER);
        //2.在添加之前,先行判断,账号是否存在,进行对account条件的判断查询,并返回一个查询的对象
        User user1 = customerUserMapper.accountIsExist(user);
        //3,如果返回的对象为空,则表示数据库没有此数据,可以进行添加
        if (user1 == null) {
            //添加user对象信息,并会返回主键到此user对象当中
            int save = customerUserMapper.save(user);
            //将返回的主键--放入dto当中
            customerDTO.setId(user.getId());
            //创建customer对象用于拷贝dto对象属性,并添加该客户信息
            Customer customer = new Customer();
            //数据拷贝,spring 框架提供的对象属性拷贝,将拥有共同部分的属性值 由对象一 拷贝到 对象二
            BeanUtils.copyProperties(customerDTO, customer);
            //根据返回值来判断是否添加成功
            int save1 = save(customer);
            if (save == 1 && save1 == 1) {
                map.put("code", 1);
                map.put("mes", "添加成功");
                return map;
            } else {
                map.put("code", 0);
                map.put("mes", "添加失败");
                return map;
            }
        }  //  OK
        //4, 走到这一步,说明user1不为空,不为空则需要判断,该用户是否被禁用或者删除
        // 0 正常 1 删除 String
        // 1 正常 2 禁用 Byte
        if (user1.getIsDeleted().equals("1")) {
            //如果账户被删除则将dto对象信息,覆盖原来的对象
            //执行覆盖操作,将user1的id作为条件,dto的属性值作为内容,经行信息覆盖
            //将user1查询到的id传递给dto,调用更新方法,更新user1内容,并恢复删除,解开禁用
            customerDTO.setId(user1.getId());
            //更新客户内容
            int restore = customerMapper.restore(customerDTO);
            int restore1 = customerUserMapper.restore(customerDTO);
            if (restore == 1 && restore1 == 1) {
                map.put("code", 1);
                map.put("mes", "添加成功");
                return map;
            }
            map.put("code", 0);
            map.put("mes", "添加失败");
            return map;
        }
        //5, 走到这一步说明,账户没有删除
        if (user1.getStatus() == 2) {
            //说明被禁用,返回消息
            map.put("code", 0);
            map.put("mes", "账户被禁用,请联系服务人员对帐号经行解禁");
            return map;
        } // OK
        //6, 走到这里说明账号既没有被删除,也没有被禁用
        map.put("code", 0);
        map.put("mes", "账号已存在,添加失败");
        return map;
    }

    /**
     * 删除客户
     * @param id
     * @return
     */ // OK 成功
    public int delete(int id){
        //删除对象前,先查询该对象是否存在,如果不存在,返回0
        User user = customerUserMapper.select(id);
        if (user == null) {
            return 0;
        }
        //当走到这一步,说明用户存在,此时判断,用户是否被逻辑删除
        //如果已经逻辑删除 返回0 , 反之,用户存在,且未被删除,执行逻辑删除后返回1
        if (user.getIsDeleted().equals("1")) {
            //走到这里,说明已删除
            return 0;
        }
        //走到这里,说明未删除
        int delete = customerUserMapper.delete(id);
        if(delete == 1){
            return 1;
        }
        return 0;
    }


    /**
     * 客户查询
     *
     * @param customer
     * @return
     */ // OK 成功
    public PageResult query(Customer customer) {
        PageHelper.startPage(customer.getPage(), customer.getPageSize());
        List<Customer> query = customerMapper.query(customer);
        PageInfo<Customer> userPageInfo = new PageInfo<>(query);
        return new PageResult(customer.getPage(), customer.getPageSize(), userPageInfo.getTotal(), userPageInfo.getList());
    }

    /**
     * 根据id查询单个用户
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> queryById(int id) {
        return customerMapper.queryById(id);
    }

    @Override
    public int accountIsExist(String account) {
        User user = new User();
        user.setAccount(account);
        User user1 = customerUserMapper.accountIsExist(user);
        if(user1!=null){
            return 1;
        }
        return 0;
    }

    /**
     * 查询客户简单的信息
     * @return
     */
    @Override
    public AjaxResult selectSimple() {
        Integer id = null;
        String roles = ((String[]) ThreadLocalUtils.getThreadLocalUtils().getCurrentUser().get("roles"))[0];
        if(roles.equals("sale")) {
            id = (int) ThreadLocalUtils.getThreadLocalUtils().getCurrentUser().get("id");
        }
        List<Map<String, Object>> list = customerMapper.selectSimple(id);

        return AjaxResult.success(list);
    }


    /**
     * 修改客户
     * @param customerDTO
     * @return
     */
    @Transactional //  OK 完成
    public int update(CustomerDTO customerDTO){
        //进行拷贝--customer拷贝dto的属性数据
        Customer customer = new Customer();
        BeanUtils.copyProperties(customerDTO, customer);
        //对客户内容进行修改
        int update = customerMapper.update(customer);
        //进行拷贝--user拷贝dto的属性数据
        User user = new User();
        BeanUtils.copyProperties(customerDTO, user);
        //公共信息的内容修改
        int update1 = customerUserMapper.update(user);
        if(update==1 && update1==1){
            return 1;
        }else{
            return 0;
        }
    }

}
